package com.wetao.module_homework.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.lib_common.bean.vo.QuestionNumShow;
import com.wetao.lib_common.greendao.DBUtils;
import com.wetao.lib_common.greendao.model.AssistantQuestion;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.MultiClickUtil;
import com.wetao.lib_common.utils.QuestionTypeUtil;
import com.wetao.lib_common.utils.StringUtil;
import com.wetao.module_homework.R;
import com.wetao.module_homework.R2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeworkObjectAdapter extends RecyclerView.Adapter{
    private List<AssistantQuestion> mData= new ArrayList<>();
    private Context mContext;
    private int mMoudle;
    public HomeworkObjectAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<AssistantQuestion> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setmMoudle(int moudle) {
        this.mMoudle = moudle;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.homework_object_item, parent, false);
            return new ObjectHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ObjectHolder) {
            ((ObjectHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    class ObjectHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_no)
        TextView tv_no;
        @BindView(R2.id.ll_choice_A)
        LinearLayout ll_choice_A;
        @BindView(R2.id.iv_choice_A)
        ImageView iv_choice_A;
        @BindView(R2.id.tv_choice_A)
        TextView tv_choice_A;
        @BindView(R2.id.ll_choice_B)
        LinearLayout ll_choice_B;
        @BindView(R2.id.iv_choice_B)
        ImageView iv_choice_B;
        @BindView(R2.id.tv_choice_B)
        TextView tv_choice_B;
        @BindView(R2.id.ll_choice_C)
        LinearLayout ll_choice_C;
        @BindView(R2.id.iv_choice_C)
        ImageView iv_choice_C;
        @BindView(R2.id.tv_choice_C)
        TextView tv_choice_C;
        @BindView(R2.id.ll_choice_D)
        LinearLayout ll_choice_D;
        @BindView(R2.id.iv_choice_D)
        ImageView iv_choice_D;
        @BindView(R2.id.tv_choice_D)
        TextView tv_choice_D;
        @BindView(R2.id.ll_choice_E)
        LinearLayout ll_choice_E;
        @BindView(R2.id.iv_choice_E)
        ImageView iv_choice_E;
        @BindView(R2.id.tv_choice_E)
        TextView tv_choice_E;
        @BindView(R2.id.ll_choice_F)
        LinearLayout ll_choice_F;
        @BindView(R2.id.iv_choice_F)
        ImageView iv_choice_F;
        @BindView(R2.id.tv_choice_F)
        TextView tv_choice_F;
        @BindView(R2.id.ll_choice_G)
        LinearLayout ll_choice_G;
        @BindView(R2.id.iv_choice_G)
        ImageView iv_choice_G;
        @BindView(R2.id.tv_choice_G)
        TextView tv_choice_G;
        @BindView(R2.id.ll_true)
        LinearLayout ll_true;
        @BindView(R2.id.iv_true)
        ImageView iv_true;
        @BindView(R2.id.tv_true)
        TextView tv_true;
        @BindView(R2.id.ll_false)
        LinearLayout ll_false;
        @BindView(R2.id.iv_false)
        ImageView iv_false;
        @BindView(R2.id.tv_false)
        TextView tv_false;

        public ObjectHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            tv_no.setText(mData.get(position).getQuestionNo() + ".");
            ll_choice_A.setVisibility(View.GONE);
            ll_choice_B.setVisibility(View.GONE);
            ll_choice_C.setVisibility(View.GONE);
            ll_choice_D.setVisibility(View.GONE);
            ll_choice_E.setVisibility(View.GONE);
            ll_choice_F.setVisibility(View.GONE);
            ll_choice_G.setVisibility(View.GONE);
            ll_true.setVisibility(View.GONE);
            ll_false.setVisibility(View.GONE);
            List<String> myAns = StringUtil.strToList(mData.get(position).getMyAnswer(), ",");
            if(QuestionTypeUtil.isChoiceQuestion(mData.get(position).getQuestionType())) {
                List<String> ans = StringUtil.strToList(mData.get(position).getOptions(), "###");
                if(ans == null) {
                    LogUtils.e("m1: 没有提供答案");
                }  else {
                    for(int i = 0; i < ans.size(); i++) {
                        if(i == 0) {
                            ll_choice_A.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("A")) {
                                iv_choice_A.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_A.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 1) {
                            ll_choice_B.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("B")) {
                                iv_choice_B.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_B.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 2) {
                            ll_choice_C.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("C")) {
                                iv_choice_C.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_C.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 3) {
                            ll_choice_D.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("D")) {
                                iv_choice_D.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_D.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 4) {
                            ll_choice_E.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("E")) {
                                iv_choice_E.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_E.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 5) {
                            ll_choice_F.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("F")) {
                                iv_choice_F.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_F.setImageResource(R.drawable.common_object_n);
                            }
                        } else if(i == 6) {
                            ll_choice_G.setVisibility(View.VISIBLE);
                            if(myAns != null && myAns.contains("G")) {
                                iv_choice_G.setImageResource(R.drawable.common_object_d);
                            } else {
                                iv_choice_G.setImageResource(R.drawable.common_object_n);
                            }
                        } else {
                            LogUtils.e("m1: 答案超过了E,不对");
                        }
                    }
                }
            } else if(QuestionTypeUtil.isTrueOrFalseQuestion(mData.get(position).getQuestionType())) {
                ll_true.setVisibility(View.VISIBLE);
                ll_false.setVisibility(View.VISIBLE);
                if(myAns != null && myAns.contains("T")) {
                    iv_true.setImageResource(R.drawable.common_object_d);
                } else {
                    iv_true.setImageResource(R.drawable.common_object_n);
                }
                if(myAns != null && myAns.contains("F")) {
                    iv_false.setImageResource(R.drawable.common_object_d);
                } else {
                    iv_false.setImageResource(R.drawable.common_object_n);
                }
            } else {
                LogUtils.e("m1: 是客观题，但不是选择题和判断题");
            }

        }
        private void clickListener(int position) {
            ll_choice_A.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("A", position);
                }
            });
            ll_choice_B.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("B", position);
                }
            });
            ll_choice_C.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("C", position);
                }
            });
            ll_choice_D.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("D", position);
                }
            });
            ll_choice_E.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("E", position);
                }
            });
            ll_choice_F.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("F", position);
                }
            });
            ll_choice_G.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("G", position);
                }
            });
            ll_true.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("T", position);
                }
            });
            ll_false.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickValue("F", position);
                }
            });
        }

        private void clickValue(String value, int position) {
            boolean ischange = false;
            List<String> myAns = StringUtil.strToList(mData.get(position).getMyAnswer(), ",");
            if(QuestionTypeUtil.isMutiChoiceQuestion(mData.get(position).getQuestionType())) {
                //多选题
                if(myAns != null && myAns.contains(value)) {
                    myAns.remove(value);
                    mData.get(position).setMyAnswer(myAns.size() == 0 ? null : StringUtil.strlistToString(myAns, ","));
                    if(mMoudle != TsdService.MODULE_SMALLTEST_DOT) {
                        AssistantQuestion question = DBUtils.getInstance(mContext).getAssistantQuestionByQuestionId(mData.get(position).getQuestionId());
                        //question.setMyAnswer(value);
                        question.setMyAnswer(mData.get(position).getMyAnswer());
                        DBUtils.getInstance(mContext).updateAssistantQuestion(question);
                    }
                    ischange = true;
                } else {
                    if(myAns == null) {
                        myAns = new ArrayList<>();
                    }
                    myAns.add(value);
                    Collections.sort(myAns, (a, b) -> {
                        return a.compareTo(b);
                    });
                    mData.get(position).setMyAnswer(StringUtil.strlistToString(myAns, ","));
                    if(mMoudle != TsdService.MODULE_SMALLTEST_DOT) {
                        AssistantQuestion question = DBUtils.getInstance(mContext).getAssistantQuestionByQuestionId(mData.get(position).getQuestionId());
                        //question.setMyAnswer(value);
                        question.setMyAnswer(mData.get(position).getMyAnswer());
                        DBUtils.getInstance(mContext).updateAssistantQuestion(question);
                    }
                    ischange = true;
                }
            }else if(QuestionTypeUtil.isChoiceQuestion(mData.get(position).getQuestionType())
                || QuestionTypeUtil.isTrueOrFalseQuestion(mData.get(position).getQuestionType())) {
                //单选题
                //判断题
                if(myAns != null && myAns.contains(value)) {
                    //不作处理
                } else {
                    mData.get(position).setMyAnswer(value);
                    if(mMoudle != TsdService.MODULE_SMALLTEST_DOT) {
                        AssistantQuestion question = DBUtils.getInstance(mContext).getAssistantQuestionByQuestionId(mData.get(position).getQuestionId());
                        question.setMyAnswer(value);
                        DBUtils.getInstance(mContext).updateAssistantQuestion(question);
                    }
                    ischange = true;
                }
            }else {
                LogUtils.e("m1: 是客观题，但不是选择题和判断题");
            }
            if(ischange) {
                notifyDataSetChanged();
            }
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
